using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using SourceSystemManager.Helper;

namespace SourceSystemManager
{
    public class DBManager
    {
        #region Variables
        OleDbConnection m_OleConnect = null;
        #endregion

        /// <summary>
        /// default constructor
        /// </summary>
        /// <param name="dbPath">path to database</param>
        public DBManager(string dbPath)
        {
            m_OleConnect = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath + ";Persist Security Info=False;");
        }

        /// <summary>
        /// open connection to database
        /// </summary>
        /// <returns>alway return true</returns>
        public bool pdbOpen()
        {
            if (m_OleConnect != null)
                m_OleConnect.Open();

            return true;
        }

        /// <summary>
        /// try open connection to database
        /// </summary>
        /// <returns>true - if connection successful; false - if connection failed</returns>
        public bool pdbTryOpen()
        {
            try
            {
                if (m_OleConnect != null)
                {
                    m_OleConnect.Open();
                    return true;
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Connection Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            return false;
        }

        /// <summary>
        /// Insert, udpate and delete data to database
        /// </summary>
        /// <param name="query">insert/udpate/delete query statement </param>
        /// <returns>true - if successful; false - if failed</returns>
        public bool pdbInsUpdDelDB(string query)
        {
            bool successed = false;
            try
            {
                var command = new OleDbCommand(query, m_OleConnect);
                command.ExecuteNonQuery();
                command.Dispose();
                successed = true;
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Operation Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            finally
            {
                if (m_OleConnect != null)
                    m_OleConnect.Close();
            }
            return successed;
        }

        /// <summary>
        /// Select data from database
        /// </summary>
        /// <param name="query">select query statement</param>
        /// <returns>true - if select successful; false - select failed</returns>
        public bool pdbSelDB(string query)
        {
            bool successed = false;
            try
            {
                var command = new OleDbCommand(query, m_OleConnect);
                OleDbDataReader reader = command.ExecuteReader();
                while(reader.Read())
                {

                }
                successed = true;
                command.Dispose();
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message, "Select Failed", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            finally
            {
                if (m_OleConnect != null)
                    m_OleConnect.Close();
            }
            return successed;
        }
    }
}
